﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Proy200Millas_BE;

namespace Proy200Millas_ADO
{
    public class RecoleccionADO
    {
        ConexionADO MiConexion = new ConexionADO();
        SqlConnection cnx = new SqlConnection();
        SqlCommand cmd = new SqlCommand();
        SqlDataReader dtr = default(SqlDataReader);
        Boolean vexito = false;

        public DataTable ListarRecoleccion()
        {
            DataSet dts = new DataSet();
            try
            {
                cnx.ConnectionString = MiConexion.GetCnx();
                cmd.Connection = cnx;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "usp_Listar_Recoleccion";
                SqlDataAdapter miada = default(SqlDataAdapter);
                miada = new SqlDataAdapter(cmd);
                miada.Fill(dts, "Recoleccion");

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return dts.Tables["Recoleccion"];
        }

        public bool InsertarRecoleccion(RecoleccionBE _recoleccionBE)
        {
            cnx.ConnectionString = MiConexion.GetCnx();
            cmd.Connection = cnx;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "usp_InsertarRecoleccion";
            //Agregamos parametros 
            try
            {
                cmd.Parameters.Add(new SqlParameter("@vnum_fae", SqlDbType.VarChar, 3));
                cmd.Parameters["@vnum_fae"].Value = _recoleccionBE.Num_fae;
                cmd.Parameters.Add(new SqlParameter("@vcod_esp", SqlDbType.VarChar, 3));
                cmd.Parameters["@vcod_esp"].Value = _recoleccionBE.Cod_esp;
                cmd.Parameters.Add(new SqlParameter("@vcan_rec", SqlDbType.Int));
                cmd.Parameters["@vcan_rec"].Value = _recoleccionBE.Can_rec;

                cnx.Open();
                cmd.ExecuteNonQuery();
                vexito = true;
            }
            catch (SqlException x)
            {
                vexito = false;
            }
            finally
            {
                if (cnx.State == ConnectionState.Open)
                {
                    cnx.Close();
                }
                // No olvidarse!!!!!!
                cmd.Parameters.Clear();
            }
            return vexito;
        }

        public bool ActualizarRecoleccion(RecoleccionBE _recoleccionBE)
        {
            cnx.ConnectionString = MiConexion.GetCnx();
            cmd.Connection = cnx;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "usp_ActualizarRecoleccion";
            //Agregamos parametros 
            try
            {
                cmd.Parameters.Add(new SqlParameter("@vnum_fae", SqlDbType.Char, 3));
                cmd.Parameters["@vnum_fae"].Value = _recoleccionBE.Num_fae;
                cmd.Parameters.Add(new SqlParameter("@vcod_esp", SqlDbType.Char, 3));
                cmd.Parameters["@vcod_esp"].Value = _recoleccionBE.Cod_esp;
                cmd.Parameters.Add(new SqlParameter("@vcan_rec", SqlDbType.Money));
                cmd.Parameters["@vcan_rec"].Value = _recoleccionBE.Can_rec;

                cnx.Open();
                cmd.ExecuteNonQuery();
                vexito = true;
            }
            catch (SqlException x)
            {
                vexito = false;
            }
            finally
            {
                if (cnx.State == ConnectionState.Open)
                {
                    cnx.Close();
                }
                // No olvidarse!!!!!!
                cmd.Parameters.Clear();
            }
            return vexito;
        }

        public bool EliminarRecoleccion(String vnum_fae, String vcod_esp)
        {
            cnx.ConnectionString = MiConexion.GetCnx();
            cmd.Connection = cnx;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "usp_EliminarRecoleccion";
            //Agregamos parametros 
            try
            {
                cmd.Parameters.Add(new SqlParameter("@vnum_fae", SqlDbType.Char, 3));
                cmd.Parameters["@vnum_fae"].Value = vnum_fae;
                cmd.Parameters.Add(new SqlParameter("@vcod_esp", SqlDbType.Char, 3));
                cmd.Parameters["@vcod_esp"].Value = vcod_esp;
                cnx.Open();
                cmd.ExecuteNonQuery();
                vexito = true;
            }
            catch (SqlException x)
            {
                vexito = false;
            }
            finally
            {
                if (cnx.State == ConnectionState.Open)
                {
                    cnx.Close();
                }
                // No olvidarse!!!!!!
                cmd.Parameters.Clear();
            }
            return vexito;

        }

        public RecoleccionBE ConsultaRecoleccion(string vnum_fae, string vcod_esp)
        {
            RecoleccionBE _recoleccionBE = new RecoleccionBE();
            try
            {
                cnx.ConnectionString = MiConexion.GetCnx();
                cmd.Connection = cnx;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "usp_Consultar_Recoleccion";
                //Agregamos el parametro para el SP
                cmd.Parameters.Add(new SqlParameter("@vnum_fae", SqlDbType.Char, 3));
                cmd.Parameters["@vnum_fae"].Value = vnum_fae;
                cmd.Parameters.Add(new SqlParameter("@vcod_esp", SqlDbType.Char, 3));
                cmd.Parameters["@vcod_esp"].Value = vcod_esp;
                cnx.Open();
                dtr = cmd.ExecuteReader();
                if (dtr.HasRows == true)
                {
                    dtr.Read();
                    var _mirecoleccion = _recoleccionBE;
                    _mirecoleccion.Num_fae = dtr.GetValue(dtr.GetOrdinal("num_fae")).ToString();
                    _mirecoleccion.Cod_esp = dtr.GetValue(dtr.GetOrdinal("num_fae")).ToString();
                    _mirecoleccion.Can_rec = Convert.ToInt32(dtr.GetValue(dtr.GetOrdinal("can_rec")));
                    dtr.Close();
                }
            }
            catch (SqlException ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (cnx.State == ConnectionState.Open)
                {
                    cnx.Close();
                }
                cmd.Parameters.Clear();
            }
            return _recoleccionBE;
        }


    }
}
